using Microsoft.Expression.Drawing.Core;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;
namespace Microsoft.Expression.Controls
{
	internal class PathStrategy : ShapeStrategy
	{
		private Path sourcePath;
		private static readonly DependencyProperty DataListenerProperty = DependencyProperty.Register("DataListener", typeof(Geometry), typeof(PathStrategy), new PropertyMetadata(new PropertyChangedCallback(GeometryStrategy.LayoutPropertyChanged)));
		private Geometry oldGeometry;
		private string oldGeometryString;
		private Geometry DataListener
		{
			get
			{
				return (Geometry)base.GetValue(PathStrategy.DataListenerProperty);
			}
			set
			{
				base.SetValue(PathStrategy.DataListenerProperty, value);
			}
		}
		public PathStrategy(LayoutPath layoutPath) : base(layoutPath)
		{
			this.sourcePath = (Path)layoutPath.SourceElement;
			base.SetListenerBinding(PathStrategy.DataListenerProperty, "Data");
		}
		public override bool HasGeometryChanged()
		{
			return this.HasGeometryChangedInternal(false);
		}
		internal bool HaveStartPointsChanged()
		{
			return this.HasGeometryChangedInternal(true);
		}
		protected override PathGeometry UpdateGeometry()
		{
			if (this.sourcePath == null || this.sourcePath.get_Data() == null)
			{
				this.oldGeometry = null;
				this.oldGeometryString = null;
				return null;
			}
			string currentGeometryString = this.GetCurrentGeometryString();
			Geometry currentGeometry = this.GetCurrentGeometry(currentGeometryString);
			if (currentGeometry == null)
			{
				this.oldGeometry = null;
				this.oldGeometryString = null;
				return null;
			}
			if (string.IsNullOrEmpty(currentGeometryString))
			{
				this.oldGeometryString = null;
			}
			else
			{
				this.oldGeometryString = currentGeometryString;
			}
			this.oldGeometry = currentGeometry.CloneCurrentValue();
			return this.oldGeometry.AsPathGeometry();
		}
		public override IList<GeneralTransform> ComputeTransforms()
		{
			IList<GeneralTransform> list = base.ComputeTransforms() ?? new List<GeneralTransform>();
			if (this.sourcePath != null && this.sourcePath.get_Data() != null)
			{
				list.Add(this.sourcePath.get_Data().get_Transform());
			}
			return list;
		}
		internal static bool StartPointsEqual(PathGeometry firstGeometry, PathGeometry secondGeomety)
		{
			if (firstGeometry == secondGeomety)
			{
				return true;
			}
			if (firstGeometry == null || secondGeomety == null)
			{
				return false;
			}
			if (firstGeometry.get_Figures().get_Count() != secondGeomety.get_Figures().get_Count())
			{
				return false;
			}
			for (int i = 0; i < firstGeometry.get_Figures().get_Count(); i++)
			{
				PathFigure pathFigure = firstGeometry.get_Figures().get_Item(i);
				PathFigure pathFigure2 = secondGeomety.get_Figures().get_Item(i);
				if (pathFigure.get_StartPoint() != pathFigure2.get_StartPoint())
				{
					return false;
				}
			}
			return true;
		}
		private bool HasGeometryChangedInternal(bool startPointsOnly)
		{
			if (this.sourcePath == null || this.sourcePath.get_Data() == null)
			{
				return this.oldGeometry != null || this.oldGeometryString != null;
			}
			string currentGeometryString = this.GetCurrentGeometryString();
			if (!string.IsNullOrEmpty(this.oldGeometryString))
			{
				return this.oldGeometryString != currentGeometryString;
			}
			Geometry currentGeometry = this.GetCurrentGeometry(currentGeometryString);
			if (startPointsOnly)
			{
				return !PathStrategy.StartPointsEqual(this.oldGeometry as PathGeometry, currentGeometry as PathGeometry);
			}
			return !GeometryHelper.GeometryEquals(this.oldGeometry, currentGeometry);
		}
		private string GetCurrentGeometryString()
		{
			PathGeometry pathGeometry = this.sourcePath.get_Data() as PathGeometry;
			if (pathGeometry != null)
			{
				return pathGeometry.ToString();
			}
			return null;
		}
		private Geometry GetCurrentGeometry(string pathString)
		{
			if (!string.IsNullOrEmpty(pathString))
			{
				return PathGeometryHelper.ConvertToPathGeometry(pathString);
			}
			return this.sourcePath.get_Data();
		}
	}
}
